package com.tqjc.area.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.area.entity.BO.ConfigInfoBO;
import com.tqjc.area.entity.Stock;
import com.tqjc.area.entity.SysConfigEntity;
import com.tqjc.area.service.SysConfigService;
import com.tqjc.area.wrapper.ConfigWrapper;
import com.tqjc.area.wrapper.StockWrapper;
import com.tqjc.common.faulthandle.enums.ConfigCacheEnum;
import com.tqjc.system.common.constant.ConfigConstant;
import com.tqjc.system.common.controller.BaseController;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 86195
 * @CreateTime: 2024-06-13  10:41
 * @Description: TODO
 * @Version: 1.0
 */
@Controller
@RequestMapping("/sys/config")
@Slf4j
public class SysConfigController extends BaseController {

    @Autowired
    private SysConfigService configService;

    @RequestMapping("/index")
    public String index(){

        return "/baotou-steel/NormalManage/ConfigManage.html";
    }


    @RequestMapping("/query")
    @ResponseBody
    public void queryPage(ConfigInfoBO configInfoBO, PageInfo<SysConfigEntity> page, HttpServletRequest request, HttpServletResponse response){

        try {
            IPage<SysConfigEntity> res =  configService.queryPage(configInfoBO,page);
            //结果处理,直接写到客户端
            ResponseUtil.write(response, new ConfigWrapper(res).wrap());
        } catch (Exception e){
            log.error("查询配置信息异常",e);
        }

    }



    @RequestMapping("/query/id")
    public String getDetail(String idKey, Model model){
        log.info("配置详情查询接口开始");
        try {
            SysConfigEntity sysConfigEntity = configService.getById(idKey);
            model.addAttribute("msgDetail",sysConfigEntity);
        } catch (Exception e){
            log.error("配置详情查询接口出现异常",e);

        }
        log.info("配置查询接口结束");
        return "/baotou-steel/NormalManage/configDetail.html";
    }

    @RequestMapping("/modify")
    @ResponseBody
    public AjaxJson modify(ConfigInfoBO configInfoBO){
        AjaxJson j = new AjaxJson();
        log.info("配置信息更新接口开始");
        try {
            SysConfigEntity sysConfigEntity = SysConfigEntity.builder().idKey(configInfoBO.getIdKey())
                    .configValue(configInfoBO.getConfigValue())
                    .configStatus(configInfoBO.getConfigStatus()).build();
            configService.updateById(sysConfigEntity);
            ConfigCacheEnum configCacheEnum = ConfigCacheEnum.getConfigCacheEnum(configInfoBO.getConfigId(), configInfoBO.getConfigType());
            if(configCacheEnum != null){
                Boolean value = true;
                String configValue = configInfoBO.getConfigValue();
                if(ConfigConstant.CONFIG_STATUS_UNEFFECTIVE.equals(configInfoBO.getConfigStatus())){
                    value = false;
                }
                configCacheEnum.refreshCache(configValue,value);
            }

        } catch (Exception e){
            log.error("配置信息更新出现异常",e);
            j.setSuccess(false);
            j.setMsg("配置信息更新出现异常");
            return j;
        }
        j.setSuccess(true);
        j.setMsg("操作成功");
        log.info("配置信息更新接口结束");
        return j;
    }


}
